package com.chen.service.Impl;

/**
 * @author chenchao

 */

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.common.Result;
import com.chen.mapper.AlbumMapper;
import com.chen.pojo.Album;
import com.chen.pojo.User;
import com.chen.service.AlbumService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@SuppressWarnings("all")
public class AlbumServiceImpl extends ServiceImpl<AlbumMapper, Album> implements AlbumService {
    @Resource
    private AlbumMapper albumMapper;

    //添加相册
    public Result<?> addAlbum(Album album) {
        album.setTime(new Date());
        // 如果是新增相册，需要辨别一下改空间名称是否存在
        if (album.getOp() > 0) {
            // 表示是空间内部上传图片接口，此时就不需要鉴别是否是创建相册
            return Result.success(albumMapper.insert(album));
        } else {
            // 此时就表示是属于创建新的相册
            int existSameCategoryByName = albumMapper.isExistSameCategoryByName(album.getName(), album.getCategory());
            if (existSameCategoryByName > 0) {
                return Result.error(-1, "该空间已经存在");
            } else {
                return Result.success(albumMapper.insert(album));
            }
        }
    }


    //删除相册
    public Result<?> deleteAlbum(int id, String scope) {
        // 校验该删除ID是否存在
        Album album = albumMapper.getAlbumById(id);
        if (ObjectUtil.isEmpty(album)) {
            Result.error(-1,"该相片不存在");
        }
        // 区分操作空间  scope 分为两个场景  第一是直接删除整个相册，第二只是删除某一张图片
        if ("album".equalsIgnoreCase(scope)) {
            // 此种场景就是删除整个相册
            return Result.success(albumMapper.deleteAllCategory(album.getName(),album.getCategory()));
        } else {
            return Result.success(albumMapper.deleteOneCategory(id));
        }
    }

    //更新相册
    public Result<?> updateAlbum(Album album) {
        album.setTime(new Date());
        return Result.success(albumMapper.updateById(album));
    }


    //查询相册
    public Result<?> findAll() {
        return Result.success(albumMapper.selectList(null));
    }


    //查询某人拥有的相册
    public Result<?> albumList(String name) {
        return Result.success(albumMapper.albumList(name));
    }

    //根据id进行查询
    public Result<?> getById(Long id) {
        return Result.success(albumMapper.selectById(id));
    }

    //统计
    public Result<?> categoryImg() {
        return Result.success(albumMapper.categoryImg());
    }


    public Result<?> categoryList(String name, String category) {
        return Result.success(albumMapper.categoryList(name,category));
    }

    public Result<?> categoryImgs(String name) {

        return Result.success(albumMapper.categoryImgs(name));
    }


}
